package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.LoginUser;
import com.platform.entity.GroupPurchaseVo;
import com.platform.entity.UserGroupPurchaseFakeDataVo;
import com.platform.entity.UserGroupPurchaseVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiGroupPurchaseService;
import com.platform.service.ApiUserGroupPurchaseFakeDataService;
import com.platform.service.ApiUserGroupPurchaseService;
import com.platform.service.ApiUserService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api(tags = "兑换码兑换")
@RestController
@RequestMapping("/api/userGroupPurchase")
public class ApiUserGroupPurchaseController extends ApiBaseAction {
    @Autowired
    private ApiUserGroupPurchaseService userGroupPurchaseService;
    @Autowired
    private ApiGroupPurchaseService groupPurchaseService;
    @Autowired
    private ApiUserService userService;
    @Autowired
    private ApiUserGroupPurchaseFakeDataService userGroupPurchaseFakeDataService;
    /**
     * 支付完成后查询拼团详情
     */
    @ApiOperation(value = "支付完成后查询拼团详情")
    @PostMapping("detail")
    public Object detail(@LoginUser UserVo loginUser) {
        Map<String,Object> resultMap = new HashMap<>();
        JSONObject jsonParams = getJsonRequest();
        String userGroupPurchaseSn=jsonParams.getString("userGroupPurchaseSn");
        Map map = new HashMap();
        map.put("userGroupPurchaseSn",userGroupPurchaseSn);
        List<UserGroupPurchaseVo> userGroupPurchaseVoList=userGroupPurchaseService.queryList(map);
        List<UserGroupPurchaseVo> newList = new ArrayList<>();
        for (int i=0;i<userGroupPurchaseVoList.size();i++){
            UserGroupPurchaseVo userGroupPurchaseVo = userGroupPurchaseVoList.get(i);
            //如果是参与人 则把参与人的id拿出来去user表查询头像
            if (userGroupPurchaseVo.getIsPromoter()==1){
                UserVo userVo=userService.queryObject(userGroupPurchaseVo.getParticipantsId());
                userGroupPurchaseVo.setAvatar(userVo.getAvatar());
            }
            newList.add(userGroupPurchaseVo);
        }
        //查询拼团商品数据
        GroupPurchaseVo groupPurchaseVo =groupPurchaseService.queryObject(userGroupPurchaseVoList.get(0).getGroupPurchaseId());
        resultMap.put("groupPurchaseInfo",groupPurchaseVo);
        //查询用户拼团数据
        resultMap.put("userGroupPurchaseList",newList);
        return toResponsSuccess(resultMap);
    }

    /**
     * endTime结束调此接口 自动拼成假数据
     */
    @ApiOperation(value = "支付完成后查询拼团详情")
    @PostMapping("createRobots")
    public Object createRobots(@LoginUser UserVo loginUser) {
        Map<String,Object> resultMap = new HashMap<>();
        JSONObject jsonParams = getJsonRequest();
        String userGroupPurchaseSn=jsonParams.getString("userGroupPurchaseSn");
        Map map = new HashMap();
        map.put("userGroupPurchaseSn",userGroupPurchaseSn);
        //根据sn查询该笔拼单谁是发起人
        List<UserGroupPurchaseVo> userGroupPurchaseVoList=userGroupPurchaseService.queryList(map);
        for (int i =0;i<userGroupPurchaseVoList.size();i++){
            UserGroupPurchaseVo userGroupPurchaseVo = userGroupPurchaseVoList.get(0);
            if (userGroupPurchaseVo.getIsPromoter()==0){
                Integer groupPurchaseId = userGroupPurchaseVoList.get(0).getGroupPurchaseId();
                //根据id查询拼团商品
                GroupPurchaseVo groupPurchaseVo=groupPurchaseService.queryObject(groupPurchaseId);
                //查询该拼团商品总共需要几个人
                int personNumber=groupPurchaseVo.getPersonNumber();
                int userGroupPurchaseSize=userGroupPurchaseVoList.size();
                int result = personNumber-userGroupPurchaseSize;
                for (int j=0;j<result;j++){
                    //查询用户有多少人
                    int userCount=userService.queryTotal(null);
                    //取随机数
                    int random=(int)(Math.random()*userCount);
                    UserVo userVo=userService.queryObject(Long.valueOf(random));
                    while (userVo==null){
                        random=(int)(Math.random()*userCount);
                         userVo=userService.queryObject(Long.valueOf(random));
                    }

                    //生成假数据
                    userGroupPurchaseVo.setGroupPurchaseId(groupPurchaseId);
                    userGroupPurchaseVo.setParticipantsId(Long.valueOf(random));
                    userGroupPurchaseVo.setIsPromoter(1);
                    userGroupPurchaseVo.setCreateTime(new Date());
                    userGroupPurchaseVo.setPayTime(new Date());
                    userGroupPurchaseVo.setUserGroupPurchaseSn(userGroupPurchaseSn);
                    userGroupPurchaseVo.setPromoterId(userGroupPurchaseVo.getPromoterId());
                    userGroupPurchaseVo.setEndTime(DateUtils.plusDay(1));
                    userGroupPurchaseVo.setRobot(0);
                    userGroupPurchaseService.save(userGroupPurchaseVo);
                }
                break;
            }
        }
        return toResponsSuccess("执行成功");
    }

    /**
     * 我的拼团
     */
    @ApiOperation(value = "我参与的拼团")
    @PostMapping("myGroupPurchase")
    public Object myGroupPurchase(@LoginUser UserVo loginUser) {
        JSONObject jsonObject=getJsonRequest();
        Integer type=jsonObject.getInteger("type");//0:全部 1:进行中 2:已完成
        Integer userId=jsonObject.getInteger("userId");
        List<UserGroupPurchaseVo> list=userGroupPurchaseService.queryMyGroupPurchase(userId,type);
        //进行中 :人没满或时间未到  已完成:时间到了或人数已满
        List newList = new ArrayList();
        for (int i=0;i<list.size();i++){
            UserGroupPurchaseVo userGroupPurchaseVo = list.get(i);
            GroupPurchaseVo groupPurchaseVo=userGroupPurchaseVo.getGroupPurchaseVo();
            Map<String,Object> map = new HashMap<>();
            map.put("userGroupPurchaseSn",userGroupPurchaseVo.getUserGroupPurchaseSn());
            List<UserGroupPurchaseVo> snList = userGroupPurchaseService.queryBySn(map);
            if (type==0){
                return toResponsSuccess(list);
            }
            if (type==1){
                if(groupPurchaseVo.getPersonNumber()> snList.size()){
                    newList.add(userGroupPurchaseVo);
                }
            }
            if (type==2){
                if(groupPurchaseVo.getPersonNumber()== snList.size()){
                    newList.add(userGroupPurchaseVo);
                }
            }
        }
        return toResponsSuccess(newList);
    }

    /**
     * 正在拼团轮播图
     */
    @ApiOperation(value = "正在拼团轮播图")
    @PostMapping("swiper")
    public Object swiper(@LoginUser UserVo loginUser) {
        JSONObject jsonObject=getJsonRequest();
        Integer groupPurchaseId=jsonObject.getInteger("groupPurchaseId");
        List<UserGroupPurchaseFakeDataVo> list=userGroupPurchaseFakeDataService.queryByGroupPurchaseId(groupPurchaseId);
        //轮播图设置三条数据
        if (list.size()<3){
            UserGroupPurchaseFakeDataVo userGroupPurchaseFakeDataVo = new UserGroupPurchaseFakeDataVo();
            for (int i=list.size();i<3;i++) {
                int userCount=userService.queryTotal(null);
                //取随机数
                int random=(int)(Math.random()*userCount);
                UserVo userVo=userService.queryObject(Long.valueOf(random));
                while (userVo==null){
                    random=(int)(Math.random()*userCount);
                    userVo=userService.queryObject(Long.valueOf(random));
                }
                //生成假数据
                userGroupPurchaseFakeDataVo.setGroupPurchaseId(groupPurchaseId);
                userGroupPurchaseFakeDataVo.setParticipantsId(Long.valueOf(random));
                userGroupPurchaseFakeDataVo.setIsPromoter(0);
                userGroupPurchaseFakeDataVo.setCreateTime(new Date());
                userGroupPurchaseFakeDataVo.setPayTime(new Date());
                userGroupPurchaseFakeDataVo.setUserGroupPurchaseSn(System.currentTimeMillis() + RandomStringUtils.randomAlphanumeric(7));
                userGroupPurchaseFakeDataVo.setPromoterId(Long.valueOf(random));
                userGroupPurchaseFakeDataVo.setEndTime(DateUtils.addHour(new Date(),(int) (Math.random() * ( 24 +6) )));
                userGroupPurchaseFakeDataVo.setRobot(0);
                userGroupPurchaseFakeDataService.save(userGroupPurchaseFakeDataVo);
            }
        }
        List newList = new LinkedList();
        //重新查询生成了的数据
        list=userGroupPurchaseFakeDataService.queryByGroupPurchaseId(groupPurchaseId);

        for (int i=0;i<list.size();i++){
            UserGroupPurchaseFakeDataVo userGroupPurchaseFakeDataVo=list.get(i);
            UserGroupPurchaseFakeDataVo groupPurchaseFakeDataVo=userGroupPurchaseFakeDataService.queryObjectByUserId(userGroupPurchaseFakeDataVo.getPromoterId());
            //查询发起人头像
            newList.add(groupPurchaseFakeDataVo);
        }
        return toResponsSuccess(newList);
    }
}
